import {require} from "../netWork";

export  const getCurrentCity = () => {
    const localCity = (localStorage.getItem('hkzf'));
    //如果本地储存没有数据，获取数据
    if (!localCity) {
        return new Promise((resolve, reject) => {  //获取定位
                var mycity = new window.BMapGL.LocalCity();

                mycity.get(async res => {
                    let city = res.name;
                    try {
                        require({
                            url: '/area/info',
                            data: {name:city}
                        }).then( async res => {

                            // console.log(res);
                            // this.setState({city: res.body.label})
                           localStorage.setItem('hkzf',  JSON.stringify(res.body));
                            console.log(res.body);
                            //resolve返回请求成功的值
                            resolve(res)
                        })
                        // const result = await axios.get(`http://192.168.1.6:8009/area/info?name=${city}`);
                        // console.log(result);
                        // localStorage.setItem('hkzf', JSON.stringify(result.body));
                        // resolve(result.data.body)
                    } catch (e) {
                        //reject返回请求失败
                        reject(e)
                    }
                })
            }
        );
        //如果有本地存储
    } else {
        // console.log(localCity);
        return Promise.resolve(localCity)
    }
};
